import axios from 'axios'
import qs from 'qs'

axios.defaults.timeout = 5000   //默认超时时间（毫秒）
axios.defaults.withCredentials = true   // 允许跨域
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'

// process.env 查看系统环境变量  可用于更改baseURL
if (process.env.NODE_ENV === 'production') {
    axios.defaults.baseURL = 'http://127.0.0.1:8000'
    // axios.defaults.baseURL = 'http://192.168.110.131/'
}else{
    axios.defaults.baseURL = 'http://127.0.0.1:8000'
  // axios.defaults.baseURL = 'http://192.168.110.131/'
}

// 响应拦截器
axios.interceptors.response.use(
    response => {
      // console.log(response)
        if (response.status === 200){
            return Promise.resolve(response)
        }else{
            return Promise.reject(response)
        }
    },
    error => {
        if(error.response.data.status_code){
            switch (error.response.data.status_code) {
                // 没有权限
                // case 403:
                //     router.replace({
                //         path: '/',
                //         query: {
                //             redirect:router.currentRoute.path
                //         }
                //     });
                //     break;
                case 500:
                    break;

            }
            return Promise.reject(error.response);
        }
    }
);


/**
 *  封装get方法
 *  @param url
 *  @param params
 *  @returns {Promise}
 */
export function get(url, params = {}) {
    return new Promise((resolve, reject) => {
        axios.get(url, {params: params})
          .then(response => {
              resolve(response.data)
          })
          .catch(err=>{
              reject(err)
          })
    })
}

/**
 *  封装post方法
 *  @param url
 *  @param data
 *  @return Promise
 */
export function post(url, data) {
    return new Promise((resolve, reject) => {
        axios.post(url, data)
          .then(response => {
              resolve(response.data);
          }, err => {
              reject(err)
          })
    })
}

export function fileUpload(url, data){
  let instance = axios.create({
    baseURL: 'http://127.0.0.1:8000/',
    timeout: 5000,
    headers: {'Content-Type': 'multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW'}
  })
  instance.interceptors.response.use(
    response => {
      // console.log(response)
      if (response.status === 200){
        return Promise.resolve(response)
      }else{
        return Promise.reject(response)
      }
    },
    error => {
      return Promise.reject(error.response);
    }
  );
  return new Promise((resolve, reject) => {
    instance.post(url, data)
      .then(response => {
        resolve(response.data)
      }, err => {
        reject(err)
      })
  })
}

export function getList(url, params = {}) {
  return new Promise((resolve, reject) => {
    axios.get(url, {params: params, paramsSerializer: params => {
        return qs.stringify(params, { indices: false })
      }})
      .then(response => {
        resolve(response.data)
      })
      .catch(err=>{
        reject(err)
      })
  })
}
